package tsou.cn.glidetest;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import tsou.cn.glidetest.adapter.ListAdapter;
import tsou.cn.glidetest.bean.ListBean;
import tsou.cn.glidetest.view.CustomLoadMoreView;

public class ListActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {

    private RecyclerView mRecyclerview;
    private SwipeRefreshLayout mSwipeRefresh;
    private ListAdapter mAdapter;
    private int mLastIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        initView();
        initRecyclerView();
        initListener();
        mAdapter.setEnableLoadMore(false);
        mSwipeRefresh.setRefreshing(true);
        fetchData(true);
    }

    private void initView() {
        mRecyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
    }

    private void initRecyclerView() {
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ListAdapter(null);
        mAdapter.isFirstOnly(false);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mRecyclerview.setAdapter(mAdapter);
    }

    private void initListener() {
        mSwipeRefresh.setOnRefreshListener(this);
        mAdapter.setOnLoadMoreListener(this, mRecyclerview);
    }

    private boolean mIsRefresh;

    private void fetchData(boolean isRefresh) {
        mIsRefresh = isRefresh;
        if (isRefresh) {
            mLastIndex = 1;
        }
        new AsyncTask<Void, Void, Void>() {

            private List<ListBean> lists = new ArrayList<>();

            @Override
            protected Void doInBackground(Void... voids) {
                SystemClock.sleep(3000);
                if (mLastIndex <= 3) {
                    // 加载数据
                    for (int i = 0; i < 10; i++) {
                        ListBean listBean = new ListBean();
                        Random random = new Random();
                        listBean.setImage("https://unsplash.it/200/200?random&" + random.nextInt(225));
                        listBean.setTitle("刘芳，20岁，河北保定人，9个月前，她辞去了北京一饭店服务员的工作，" +
                                "来到王庆坨镇一家自行车生产上班，只因为好朋友邀请她，" +
                                "“快来，这里现在有好多共享单车的订单”。" +
                                "“她说他们公司忙都忙不过来，几乎天天在加班。”刘芳说，好友告诉她，" +
                                "加班很累，但收入可观，多的时候一个月能挣到六七千");
                        listBean.setSource("腾讯网");
                        listBean.setStar(random.nextInt(100));
                        listBean.setEvn(random.nextInt(100));
                        lists.add(listBean);
                    }
                } else {
                    lists.clear();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                if (mIsRefresh) {
                    mAdapter.setNewData(lists);
                    mSwipeRefresh.setRefreshing(false);
                    mAdapter.setEnableLoadMore(true);
                } else {
                    if (lists != null) {
                        if (lists.size() == 0) {
                            mAdapter.loadMoreEnd();
                        } else {
                            mAdapter.addData(lists);
                            mAdapter.loadMoreComplete();
                        }
                    }
                }
                mLastIndex++;
            }
        }.execute();
    }

    @Override
    public void onRefresh() {
        mAdapter.setEnableLoadMore(false);
        fetchData(true);
    }

    @Override
    public void onLoadMoreRequested() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setEnabled(false);
            fetchData(false);
            mSwipeRefresh.setEnabled(true);
        }
    }
}
